package com.best.smart.logfile;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class LogDetailActivity extends Activity {


    //int[] iconArr = {R.drawable.};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logdetail);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView tv_info = findViewById(R.id.tv_info);
        File file = new File(getIntent().getStringExtra("log"));
        int type = getIntent().getIntExtra("showEvent",0);

        tv_info.setText(readFileContent(file,type));

    }

    private String readFileContent(File file,int type) {
        List<String> lines = new ArrayList<>();

        // 读取文件的所有行到列表中
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (type == 0){
                    if (!line.contains("upload event:")){
                        lines.add(line);
                    }
                }else {
                    if (line.contains("upload event:")){
                        lines.add(line);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 反转列表
        Collections.reverse(lines);
        // 将反转后的内容拼接成字符串
        StringBuilder contentBuilder = new StringBuilder();
        for (String line : lines) {
            contentBuilder.append(line).append("\n");
        }

        return contentBuilder.toString();
    }


}
